package com.linkallcloud.core.www.utils;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

import com.linkallcloud.core.log.Log;
import com.linkallcloud.core.log.Logs;

public class HttpClientFactory {
    private static final Log log = Logs.get();

    private static HttpClientFactory hcf = new HttpClientFactory();
    private static OkHttpUtils client = OkHttpUtils.me();

    public static HttpClientFactory me(boolean sslHostnameVerify) {
        return hcf;
    }

    /**
     * @param url
     * @return
     */
    public String get(String url) {
        CompletableFuture<String> f = client.get(url);
        try {
            return f.get();
        } catch (InterruptedException e) {
            log.error(e.getMessage(), e);
        } catch (ExecutionException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    /**
     * @param url
     * @param jsonStr
     * @return
     */
    public String post(String url, String jsonStr) {
        CompletableFuture<String> f = client.postJson(url, jsonStr);
        try {
            return f.get();
        } catch (InterruptedException e) {
            log.error(e.getMessage(), e);
        } catch (ExecutionException e) {
            log.error(e.getMessage(), e);
        }
        return null;
    }

    public static void main(String[] args) {
        // 1.
        String response = HttpClientFactory.me(true).get("https://www.baidu.com");
        System.out.println(response);

        // 2.
        String response2 = HttpClientFactory.me(false).get("https://www.baidu.com");
        System.out.println(response2);
    }

}
